<?php
class Upload extends AppModel{
	function check_image($image){
		if($image==""){
			return true;
		}
	
		$valid_ext = array('JPG','jpg','png','gif','bmp');
		
		$pathinfo = pathinfo($image);
		
	
		@$ext = $pathinfo['extension'];
		
		if(in_array($ext,$valid_ext)){
			return true;
		}else{
			return false;
		}
	}
	
	function upload_image($image,$dir){

		if(count($image)==0 || $image['error']==4){
			
			$upload = array('status'=>'1','name'=>'');
			
			// print_r($upload);
			//exit;
			return $upload ;
		}
		
		$name = $image['name'];
		$check_image = $this->check_image($name);
		if($check_image){
			$image_name = $this->set_name($name);
			$upload = move_uploaded_file($image['tmp_name'],$dir.$image_name);
			
			$upload = array('status'=>'1','name'=>$image_name);
		}else{
			$upload = array('status'=>'0','name'=>'');
		}
		
		
		return $upload ;
	}
	
	function set_name($name){
		$pathinfo = pathinfo($name);
		
		$ext = $pathinfo['extension'];
		$custom_name = $this->Generator(10);
		
		$image_name = $custom_name.".".$ext ;
		
		return $image_name;
		
	}
	
	function Generator($lenth)
	{
		$number=array("A","B","C","D","E","F","G","H","I","J","K","L","N","M","O","P","Q","R","S","U","V","T","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0");
		
		for($i=0; $i<$lenth; $i++)
		{
			$rand_value=rand(0,61);
			$rand_number=$number["$rand_value"];
		
			if(empty($con))
			{ 
			$con=$rand_number;
			}
			else
			{
			$con="$con"."$rand_number";}
			}
		return $con;
	}
}
?>